/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.base.Preconditions;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.pilot.app.command.cmo.UpdateDeptCmd;
import com.itgrail.pilot.app.service.DepartmentService;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.DepartmentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


@CmdHandler
public class UpdateDeptCmdExe implements CommandExecutorI<ResultData, UpdateDeptCmd> {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private DepartmentService departmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData execute(UpdateDeptCmd cmd) {
        check(cmd);
        LambdaUpdateWrapper<DepartmentDO> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.set(DepartmentDO::getName, cmd.getName());
        updateWrapper.set(DepartmentDO::getParentId, cmd.getParentId());
        updateWrapper.set(DepartmentDO::getLeaderId, cmd.getLeaderId());
        updateWrapper.set(DepartmentDO::getLeaderName, cmd.getLeaderName());
        updateWrapper.set(DepartmentDO::getIsCount, cmd.getIsCount());
        updateWrapper.set(DepartmentDO::getDisplayPath, departmentService.getFullDeptPath(cmd.getParentId()) + "/" + cmd.getName());
        updateWrapper.eq(DepartmentDO::getId, cmd.getId());
        departmentMapper.update(null, updateWrapper);
        return ResultData.success(null);
    }

    protected boolean check(UpdateDeptCmd cmd) {
        Preconditions.checkNotNull(cmd, "cmd不能为null");
        Preconditions.checkArgument(cmd.getId() != null, "id不能为null");
        Preconditions.checkArgument(cmd.getParentId() != null, "上级部门不能为空");
        Preconditions.checkArgument(StrUtil.isNotBlank(cmd.getName()), "部门名称不能为空");
        DepartmentDO departmentDO = departmentMapper.selectById(cmd.getId());
        Preconditions.checkNotNull(departmentDO, "部门不存在");

        DepartmentDO parentDepartmentDO = departmentMapper.selectById(cmd.getParentId());
        Preconditions.checkNotNull(parentDepartmentDO, "上级部门不存在");

        Preconditions.checkState(!String.valueOf(1).equals(departmentDO.getIsDeleted()), "部门不存在");
        Preconditions.checkState(!Integer.valueOf(0).equals(departmentDO.getEnabled()), "当前部门状态不能操作");
        return true;
    }

}
